package com.youdianstar.app.ui.account

import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.lifecycle.Observer
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.adapter.PhoneCodeAdapter
import com.youdianstar.app.databinding.ActivityPhoneCodeBinding
import com.youdianstar.app.entity.CodeEntity
import com.youdianstar.app.entity.CodeListRes
import com.youdianstar.app.support.imp.CodeCallback
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.network.BaseApiStateObserver
import com.youdianstar.app.common.util.ToastUtil
import com.gyf.immersionbar.ImmersionBar
import org.koin.androidx.viewmodel.ext.android.viewModel


/**
 * File: PhoneCodeActivity
 * Author: 82149 Create: 2021/9/27 17:10
 * Changes (from 2021/9/27)
 * --------------------------------------------------
 * description: 国家码页面
 * ---------------------------------------------------
 *
 */

class PhoneCodeActivity :BaseActivity<ActivityPhoneCodeBinding, PhoneCodeViewModel>(){
    override fun layoutId(): Int {
       return R.layout.activity_phone_code
    }

    override fun initVariableId(): Int {
      return BR.viewModel
    }

    override fun initViewModel(): PhoneCodeViewModel? {
        val model : PhoneCodeViewModel by viewModel()
        return model
    }

    private val codeAdapter by lazy { PhoneCodeAdapter() }

    override fun initViewLayout() {
        super.initViewLayout()
        ImmersionBar.with(this)
            .statusBarColor(R.color.white)
            .statusBarDarkFont(true)
            .titleBarMarginTop(mBinding.rlTitlebar)
            .init()
    }

    override fun initData() {
        super.initData()
        mBinding.run {
            codeRcvList.adapter = codeAdapter
            codeAdapter.setCallback(object : CodeCallback {
                override fun onItemSelected(data: CodeEntity) {
                    val i = Intent()
                    i.putExtra(Extra.IT_ID, data.id)
                    i.putExtra(Extra.IT_CODE, data.code.toString())
                    setResult(Activity.RESULT_OK, i)
                    finish()
                }
            })
            etSearch.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                    mViewModel.findSearch(this@PhoneCodeActivity,s.toString())
                }

                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

                }

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

                }
            })
        }

        mViewModel.getPhoneCodeData()

    }

    override fun initLiveObserver() {
        super.initLiveObserver()
        // 只监听本地数据变化，不再监听网络请求
        mViewModel.phoneCodeAdapterData.observe(this, Observer {
            codeAdapter.setData(it)
        })
    }


    override fun initClickEvent() {

    }

}